//
//  SZNetworkTools.m
//  Weibo
//
//  Created by Zahi on 2017/7/25.
//  Copyright © 2017年 Zahi. All rights reserved.
//

#import "SZNetworkTools.h"

@implementation SZNetworkTools


+ (instancetype)sharedTool
{
    static SZNetworkTools *tools;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tools = [SZNetworkTools manager];
        
        tools.responseSerializer.acceptableContentTypes = [tools.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"text/html"]];
        
    });
    
    return tools;
}


#pragma mark - 封装GET和POST请求

/**
 封装GET和POST请求
 
 @param type 请求类型
 @param URLString 请求链接
 @param params 请求参数
 @param completionBlock 完成后回调
 */
- (void)requset:(RequestType )type URLString:(NSString *)URLString params:(id)params completion:(void(^)(id response, NSError *error))completionBlock
{
    if (type == RequestTypeGet) { //get请求
        [self GET:URLString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            completionBlock(responseObject, nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            completionBlock(nil, error);
        }];
    } else { //post 请求
        
        [self POST:URLString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            completionBlock(responseObject, nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            completionBlock(nil, error);
        }];
    }
}


#pragma mark - 首页相关的接口
#pragma mark 获取当前登录用户及其所关注（授权）用户的最新微博
- (void)statuesWithToken:(NSString *)access_token callBack:(void(^)(id responce, NSError *error))callBack
{
    NSString *URLString = @"https://api.weibo.com/2/statuses/home_timeline.json";
    
    NSDictionary *params = @{@"access_token" : access_token};
    
    [self requset:RequestTypeGet URLString:URLString params:params completion:callBack];
}


#pragma mark - OAuth授权相关接口
#pragma mark 获取用户信息
- (void)accoundInfoWithToken:(NSString *)access_token uid:(NSInteger)uid callBack:(void(^)(id responce, NSError *error))callBack
{
    NSString *URLSting = @"https://api.weibo.com/2/users/show.json";
    
    NSDictionary *params = @{
                             @"access_token" : access_token,
                             @"uid" : @(uid)
                             };
    
    [self requset:RequestTypeGet URLString:URLSting params:params completion:callBack];
}

#pragma mark 获取令牌
- (void)accessTokenWithCode:(NSString *)code callBack:(void(^)(id responce, NSError *error))callBack
{
    NSString *URLString = @"https://api.weibo.com/oauth2/access_token";
    
    NSDictionary *params = @{
                             @"client_id" : AppKey,
                             @"client_secret" : AppSecret,
                             @"grant_type" : @"authorization_code",
                             @"code" : code,
                             @"redirect_uri" : Redirect_uri
                             };
    
    [self requset:RequestTypePost URLString:URLString params:params completion:callBack];
}

@end
